package org.example.practiceexamples.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dox.Lab;
import org.example.practiceexamples.dox.User;
import org.example.practiceexamples.exception.Code;
import org.example.practiceexamples.service.AdminService;
import org.example.practiceexamples.vo.ResultVO;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/admin/")
public class AdminController {
    private final AdminService adminService;
    private final PasswordEncoder passwordEncoder;


    @PostMapping("saveUser")
    public ResultVO saveUser(@RequestBody User user) {
        adminService.saveTeacher(user);
        return ResultVO.ok();
    }

    @PostMapping("saveLab")
    public ResultVO saveLab(@RequestBody Lab lab) {
        adminService.saveLab(lab);
        return ResultVO.ok();
    }

    @PostMapping("saveCourse")
    public ResultVO saveCourse(@RequestBody Course course) {
        adminService.saveCourse(course);
        return ResultVO.ok();
    }

    @PostMapping("saveAppointment")
    public ResultVO saveAppointment(@RequestBody Appointment appointment) {
        adminService.saveAppointment(appointment);
        return ResultVO.ok();
    }

    @GetMapping("getCourses")
    public ResultVO getCourses() {
        List<Course> courses = adminService.findAllCourses();
        return ResultVO.success(courses,"查询成功!");
    }

    @GetMapping("getAppointments")
    public ResultVO getAppointments() {
        List<Appointment> appointments = adminService.findAllAppointments();
        return ResultVO.success(appointments,"查询成功！");
    }

    @GetMapping("getLabs")
    public ResultVO getLabs() {
        List<Lab> labs = adminService.findAllLabs();
        return ResultVO.success(labs,"查询成功!");
    }

    @GetMapping("getUsers")
    public ResultVO getUsers() {
        List<User> users = adminService.findAllUsers();
        return ResultVO.success(users,"查询成功!");
    }

    @GetMapping("getUser/{uid}")
    public ResultVO getUser(@PathVariable String uid) {
        User user = adminService.findUserById(uid);
        return ResultVO.success(user,"查询成功!");
    }

    @GetMapping("getLab/{lname}")
    public ResultVO getLab(@PathVariable String lname) {
        Lab lab = adminService.findLabByLname(lname);
        return ResultVO.success(lab,"查询成功!");
    }

    @GetMapping("getCourse/{cid}")
    public ResultVO getCourse(@PathVariable String cid) {
        Course course = adminService.findCourseById(cid);
        return ResultVO.success(course,"查询成功!");
    }

    @GetMapping("getAppointment/{aid}")
    public ResultVO getAppointment(@PathVariable String aid) {
        Appointment a = adminService.findAppointmentById(aid);
        return ResultVO.success(a,"查询成功!");
    }


    @PostMapping("updateLabMessage/{lname}")
    public ResultVO updateLabMessage(@PathVariable String lname, @RequestBody Lab lab) {
        try{
            if(lab.getDescription()!=null){
                adminService.updateDescription(lname, lab.getDescription());
            }
            if(lab.getState() != null){
                adminService.updateState(lab.getState(), lname);
            }
            if(lab.getQuantity() != null){
                adminService.updateQuantity(lab.getQuantity(), lname);
            }
            return ResultVO.success("修改成功!");

        }
        catch (Exception e){
            log.error("Error updateMessage", e);
            return ResultVO.error(Code.ERROR, "Failed to updateMessage");
        }
    }

        @PostMapping("updateUserMessage/{uid}")
    public ResultVO updateUserMessage(@PathVariable String uid, @RequestBody User user) {
        try{
            if(user.getStatus()!=null){
                adminService.updateStatus(uid,user.getStatus());
            }
            if(user.getDescription()!=null){
                adminService.updateUserDescription(uid, user.getDescription());
            }
            if (user.getRole()!=null){
                adminService.updateRole(uid, user.getRole());
            }
            if(user.getPassword()!=null){
                adminService.updatePassword(uid, user.getPassword());
            }
            if(user.getProfession()!=null){
                adminService.updateProfession(uid, user.getProfession());
            }
            return ResultVO.success("修改成功！");
        }
        catch(Exception e){
            log.error("Error updateUserMessage", e);
            return ResultVO.error(Code.ERROR, "Failed to updateUserMessage");
        }
    }

    @PatchMapping("reset/password")
    public ResultVO resetPassword(@RequestBody String account) {
        adminService.updatePassword(account, passwordEncoder.encode(account));
        return ResultVO.ok();
    }

    @DeleteMapping("deleteUser/{uid}")
    public ResultVO deleteUser(@PathVariable String uid) {
        try{
            adminService.deleteUser(uid);
            return ResultVO.success("删除成功!");
        }
        catch (Exception e){
            log.error("Error deleteUser", e);
            return ResultVO.error(Code.ERROR, "Failed to deleteUser");
        }
    }

    @DeleteMapping("deleteCourse/{cid}")
    public ResultVO deleteCourse(@PathVariable String cid) {
        try{
            adminService.deleteCourse(cid);
            return ResultVO.success("删除成功!");
        }
        catch (Exception e){
            log.error("Error deleteCourse", e);
            return ResultVO.error(Code.ERROR, "Failed to deleteCourse");
        }
    }

    @DeleteMapping("deleteLab/{lname}")
    public ResultVO deleteLab(@PathVariable String lname) {
        try{
            adminService.deleteLab(lname);
            return ResultVO.success("删除成功!");
        }
        catch (Exception e){
            log.error("Error deleteLab", e);
            return ResultVO.error(Code.ERROR, "Failed to deleteLab");
        }
    }

    @DeleteMapping("deleteAppointment/{aid}")
    public ResultVO deleteAppointment(@PathVariable String aid) {
        try{
            adminService.deleteAppointmentById(aid);
            return ResultVO.success("删除成功!");
        }
        catch (Exception e){
            log.error("Error deleteAppointment", e);
            return ResultVO.error(Code.ERROR, "Failed to deleteAppointment");
        }
    }

    @DeleteMapping("deleteAllAppointments")
    public ResultVO deleteAllAppointments() {
        List<Appointment> appointments = adminService.findAllAppointments();
        for(Appointment appointment : appointments){
            try{
                adminService.deleteAppointmentById(appointment.getId());
            }
            catch (Exception e){
                log.error("Error saving appointment", e);
                return ResultVO.error(Code.ERROR, "Failed to delete appointment");
            }
        }
        return ResultVO.success("删除成功");
    }




}
